package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.mapper.YgBargainDetailMapper;
import com.ygqh.baby.model.EditBargainDetailReq;
import com.ygqh.baby.po.YgBargainDetail;
import com.ygqh.baby.po.YgBargainDetailExample;
import com.ygqh.baby.service.YgBargainDetailService;
import com.ygqh.baby.utils.ArraysUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class YgBargainDetailServiceImpl implements YgBargainDetailService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private YgBargainDetailMapper ygBargainDetailMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int editBargainDetail(List<EditBargainDetailReq> detailList) {
        Assert.notEmpty(detailList, "detailList can't be null or empty");
        Long bargainId = detailList.get(0).getBargainId();
        String handler = detailList.get(0).getHandler();
        Date updateTime = detailList.get(0).getUpdateTime();
        List<YgBargainDetail> oldList = this.findByBargainId(bargainId);
        List<Long> oldProductIdList = oldList.stream().map(YgBargainDetail::getProductId).collect(Collectors.toList());
        List<Long> newProductIdList = detailList.stream().map(EditBargainDetailReq::getProductId).distinct().collect(Collectors.toList());
        ArraysUtil.removeRepet(newProductIdList, oldProductIdList);
        Map<Long, List<YgBargainDetail>> collect = oldList.stream().collect(Collectors.groupingBy(YgBargainDetail::getProductId));

        List<YgBargainDetail> addList = new ArrayList<>();
        List<YgBargainDetail> updateList = new ArrayList<>();

        detailList.forEach(req -> {
            if (newProductIdList.contains(req.getProductId())) {
                YgBargainDetail detail = new YgBargainDetail();
                BeanUtils.copyProperties(req, detail);
                detail.setCreateBy(handler);
                detail.setCreateTime(updateTime);
                detail.setUpdateTime(updateTime);
                detail.setUpdateBy(handler);
                detail.setStatus(DataStatus.Valid.name());
                addList.add(detail);
            } else if (!oldProductIdList.contains(req.getProductId())) {
                YgBargainDetail detail = collect.get(req.getProductId()).get(0);
                detail.setUpdateTime(updateTime);
                detail.setUpdateBy(handler);
                detail.setMinCutCount(req.getMinCutCount());
                detail.setMaxCutCount(req.getMaxCutCount());
                detail.setStockQuantity(req.getStockQuantity());
                detail.setRealStock(req.getRealStock());
                updateList.add(detail);
            }
        });
        if (CollectionUtils.isNotEmpty(oldProductIdList)) {
            this.deleteByProductIds(bargainId, oldProductIdList, handler, updateTime);
        }
        if (CollectionUtils.isNotEmpty(addList)) {
            ygBargainDetailMapper.addBatch(addList);
        }
        if (CollectionUtils.isNotEmpty(updateList)) {
            ygBargainDetailMapper.updateBatchByIdSelective(updateList);
        }

        return 1;
    }

    private void deleteByProductIds(Long bargainId, List<Long> pidList, String handler, Date updateTime) {
        YgBargainDetailExample example = new YgBargainDetailExample();
        example.createCriteria().andBargainIdEqualTo(bargainId).andProductIdIn(pidList).andStatusEqualTo(DataStatus.Valid.name());

        YgBargainDetail record = new YgBargainDetail();
        record.setStatus(DataStatus.Delete.name());
        record.setUpdateBy(handler);
        record.setUpdateTime(updateTime);
        ygBargainDetailMapper.updateByExampleSelective(record, example);
    }

    @Override
    public List<YgBargainDetail> findByBargainId(Long bargainId) {
        YgBargainDetailExample example = new YgBargainDetailExample();
        example.createCriteria().andBargainIdEqualTo(bargainId).andStatusEqualTo(DataStatus.Valid.name());

        return ygBargainDetailMapper.selectByExample(example);

    }

    @Override
    public List<Long> getProductIdsInOtherBargain(Long excludedBargainId, List<Long> productIdList,Date startDate, Date endDate) {
        if (CollectionUtils.isEmpty(productIdList)) {
            return Collections.emptyList();
        }
        return ygBargainDetailMapper.getProductIdsInOtherBargain(excludedBargainId, productIdList,startDate,endDate);
    }

    @Override
    public List<YgBargainDetail> findDetailByBargainId(Long id) {
        return ygBargainDetailMapper.selectDetailByBargainId(id);
    }
}
